import {createRouter, createWebHashHistory, useRoute} from "vue-router";
import {routes} from "./route.list";
import eventBus from "../eventBus";
import {watch} from "vue";

/**
 * 路由
 */
export const router = createRouter({
    history: createWebHashHistory(),
    routes: routes,
});

/**
 * 路由守卫
 */
router.beforeEach((to, _, next) => {
    if (to.matched.length === 0) {
        next({path: "/404"});
    } else {
        next();
    }
});

/**
 * 路由监听
 */
eventBus.on("loginSuccess", () => router.push("/user"));
eventBus.on("logout", () => {
    router.push("/login");
});
